package com.shelpe.services.sinbad.entity.jpa;

import java.util.Date;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import com.shelpe.services.sinbad.service.utils.EntityUtils.DriveTypeConverter;

import lombok.Data;

/**
 * @description 我的养分宝贝详情
 * 
 * @author letian@shelpe.com
 * @date 2017年8月28日 下午5:37:20
 */
@Entity(name = "mobile_adgroup_detail")
@Data
public class MobileAdgroupDetail {
	
	@Id
	@Column(name="id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long autoID;
	
	@Convert(converter=StatusConverter.class)
	private Status status;
	
	@Column(name="adgroup_id")
	private long adgroupID;
	
	@Column(name="campaign_id")
	private long campaignID;
	
	@Column(name="user_id")
	private long userID;
	
	@Column(name="created_time")
	private Date createdTime;
	
	@Convert(converter = DriveTypeConverter.class)
	@Column(name = "drive_type")
	private DriveType driveType = DriveType.auto;
	
	public static enum Status{
		starting(1), finish(2), exception(3);
		private int value;

		private Status(int v) {
			this.value = v;
		}

		public int getValue() {
			return this.value;
		}
	}
	
	public static class StatusConverter implements AttributeConverter<Status, Integer> {

		@Override
		public Integer convertToDatabaseColumn(Status attribute) {
			return attribute != null ? attribute.getValue() : Status.starting.getValue();
		}

		@Override
		public Status convertToEntityAttribute(Integer dbData) {
			Status status = Status.starting;
			for (Status s : Status.values()) {
				if (s.getValue() == dbData) {
					status = s;
					break;
				}
			}
			return status;
		}
	}
	
	public void from(Adgroup adgroup) {
		this.adgroupID = adgroup.getAutoID();
		this.campaignID = adgroup.getCampaignID();
		this.userID = adgroup.getUserID();
		this.status = Status.starting;
		this.createdTime = new Date();
		this.driveType = adgroup.getDriveType();
	}
}
